package com.taa.domain.trade.service.login;

import com.taa.domain.trade.adapter.repository.ITradeRepository;
import com.taa.domain.trade.model.entity.UserEntity;
import com.taa.domain.trade.service.IUserService;
import com.taa.types.enums.ResponseCode;
import com.taa.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: taa
 * @Desc:
 * @Create: 2025-06-02 19:55
 **/
@Slf4j
@Service
public class UserService implements IUserService {

    @Resource
    private ITradeRepository repository;


    @Override
    public boolean login(String userName, String password) {
        log.info("用户登录校验 userName: {} password: {}", userName, password);
        UserEntity user = repository.queryUserByUserName(userName);
        if (user == null) {
            log.info("用户不存在 userName: {}", userName);
            throw new AppException(ResponseCode.USER_NOT_EXIST.getCode(), ResponseCode.USER_NOT_EXIST.getInfo());
        }
        if (!user.getPassword().equals(password)) {
            log.info("用户密码错误 userName: {}", userName);
            throw new AppException(ResponseCode.ERROR_PASSWORD.getCode(), ResponseCode.ERROR_PASSWORD.getInfo());
        }
        log.info("用户登录校验成功 userName: {}", userName);
        return true;
    }
}

